var repayDetailData,repayTotal=0,repayRealDate,repayType;
$(document).ready(function() {
	//查询详情
	$.ajax({
		url:'sys/LoanExt/findRepaymentDetailForSettleAccounts',
		data:{'queryProxy[id]':_param_id},
		type:'post',
		dataType:'json',
		async:false,
		success:function(result){
			if(typeof(result)!='undefined'){
				repayDetailData = result;
			}
		},
		complete:function(){
		}
	});
	//初始化贷款详情span
	_set_lable_text ('#loanRepaymentdetail tr td span ','name','',false,repayDetailData);
	//初始化贷款回款信息
	var url = 'sys/LoanExt/findRepayment?queryProxy[id]='+repaymentId;
	_set_lable_text ('#loanRepayment ul.forminfo li span ','name',url,false);
	//初始化其他
	initOther();
	initForm();
	//初始化下拉框
	$('#repayType').mSelect({
		myClass:'easyui-validatebox',
		width:308,
		baseCode:'Loan_Close_Account_Mode_Code',
		isAllSelect:false,
		dataOptions:'required:true'
	});
	$('._class_btn_add').click(function(){
		$.dialog({
			id:'add_child_LoanRepaymentLog',
			lock:true,
			title:'新增收费纪录',
			width:500,
			height:400,
			content:'url:'+parent.basePath+'jsp/loan/loan_repayment_income.jsp?loanId='+loanId+'&repaymentId='+repaymentId+'&repaymentLogDetailId='+_param_id,
			close: function(){
				top._do_goto_right('jsp/loan/loan_repayment_detail_edit.jsp?id='+_param_id+'&loanId='+loanId+'&repaymentId='+repaymentId);
			}
		}).max();
	});
});

var initOther = function(){
	//设置今日应收值
	$.ajax({
		url:'sys/LoanExt/todayRepaymentSum',
		data:{'queryProxy[repaymentId]':repaymentId,'queryProxy[deallineNumber_LessThanOrEqual]':repayDetailData.deadlineNumber,'queryProxy[repayType]':'1'},
		type:'post',
		dataType:'json',
		async:false,
		success:function(result){
			if(typeof(result)!='undefined'){
				_set_lable_text ('#currentRepayDetailSum span ','name','',false,result);
			}
		},
		complete:function(){
		}
	});
	
	//设置提前应收值
	$.ajax({
		url:'sys/LoanExt/computerRepaymentDetail',
		data:{'repaymentId':repaymentId,'repayType':'3','id':_param_id,'repayRealDate':top.nowDate},
		type:'post',
		dataType:'json',
		async:false,
		success:function(result){
			if(typeof(result)!='undefined'){
				_set_lable_text ('#computerTheRepayDetailSum2 span ','name','',false,result.data);
			}
		},
		complete:function(){
		}
	});

	//设置收款详情查询条件
	var arrayLogs  =  new Array;
	arrayLogs.push({name:'beanProxy[enable]',value:0});
	arrayLogs.push({name:'beanProxy[loanId]',value:loanId});
	arrayLogs.push({name:'beanProxy[repaymentDetailId]',value:_param_id});
	initLoanRepaymentLog(arrayLogs);

};

var currentRepayDetailClick = function(){
	$.dialog({
		id:'today_child_LoanRepaymentDetail',
		lock:true,
		title:'今日应收详情',
		width:500,
		height:400,
		content:'url:'+parent.basePath+'jsp/loan/loan_today_loanRepaymentDetail.jsp?id='+_param_id+'&loanId='+loanId+'&repaymentId='+repaymentId,
		close: function(){
		}
	}).max();
};

var advanceRepayDetailClick = function(){
	$.dialog({
		id:'advance_child_LoanRepaymentDetail',
		lock:true,
		title:'提前应收详情',
		width:500,
		height:400,
		data:{'repayRealDate':top.nowDate,'id':_param_id,'loanId':loanId,'repaymentId':repaymentId,'repayType':'3','deadlineNumber':repayDetailData.deadlineNumber},
		content:'url:'+parent.basePath+'jsp/loan/loan_advance_loanRepaymentDetail.jsp',
		close: function(){
		}
	}).max();
}


var computerRepayDetailClick = function(){

	if(_is_empty(repayRealDate)){
		$.dialog.alert("计算因子：结算日期不能为空!");
		return false;
	}
	
	if(_is_empty(repayType)){
		$.dialog.alert("计算因子：结算方式不能为空!");
		return false;
	}
	
	if('1' == repayType){
		$.dialog({
			id:'computer_child_LoanRepaymentDetail',
			lock:true,
			title:'计算应收详情',
			width:500,
			height:400,
			data:{'repayRealDate':repayRealDate,'id':_param_id,'loanId':loanId,'repaymentId':repaymentId,'repayType':repayType,'deadlineNumber':repayDetailData.deadlineNumber},
			content:'url:'+parent.basePath+'jsp/loan/loan_computer_loanRepaymentDetail.jsp',
			close: function(){
			}
		}).max();
	}else{
		$.dialog({
			id:'computer_child_LoanRepaymentDetail',
			lock:true,
			title:'计算应收详情',
			width:500,
			height:400,
			data:{'repayRealDate':top.nowDate,'id':_param_id,'loanId':loanId,'repaymentId':repaymentId,'repayType':'3','deadlineNumber':repayDetailData.deadlineNumber},
			content:'url:'+parent.basePath+'jsp/loan/loan_advance_loanRepaymentDetail.jsp',
			close: function(){
			}
		}).max();
	}
	
};

var submitRepaymentLog = function(){
	$('#_form_repayment_log_default_').submit();
};

var clearingAccount1 = function(){
	var repayType="1";
//	if(_is_empty(repayRealDate)){
//		$.dialog.alert("结算日期不能为空!");
//		return false;
//	}
//	if(_is_empty(repayType)){
//		$.dialog.alert("结算方式不能为空!");
//		return false;
//	}
	$('#clearRepayType').val(repayType);
	//$('#clearRepayRealDate').val(repayRealDate);
	$('#clear_account_defult').submit();
};

var clearingAccount2 = function(){
	var repayType=3;
//	if(_is_empty(repayType)){
//		$.dialog.alert("结算方式不能为空!");
//		return false;
//	}
//	if(repayType!=2 ||repayType!=3){
//		$.dialog.alert("结算方式不只能为提前结清!");
//		return false;
//	}
	$('#clearRepayType').val(repayType);
	$('#clear_account_defult').submit();
};

var resetRepaymentLog = function(obj){
	$(obj).attr('disabled','disabled');
	clearFromData();
	$(obj).removeAttr('disabled');
};

var computerRepaymentDetail = function(obj){
	repayRealDate = $('#repayRealDate').val();
	repayType=$('#repayType').val();
	if(_is_empty(repayRealDate)){
		$.dialog.alert("计算因子：结算日期不能为空!");
		return false;
	}
	if(_is_empty(repayType)){
		$.dialog.alert("计算因子：结算方式不能为空!");
		return false;
	}

	//设置计算应收
	$.ajax({
		url:'sys/LoanExt/computerRepaymentDetail',
		data:{'repaymentId':repaymentId,'repayType':repayType,'id':_param_id,'repayRealDate':repayRealDate},
		type:'post',
		dataType:'json',
		async:false,
		success:function(result){
			if(typeof(result)!='undefined'){
				_set_lable_text ('#computerTheRepayDetailSum1 span ','name','',false,result.data);
//				if(1 == repayType){
//					_set_lable_text ('#computerTheRepayDetailSum1 span ','name','',false,result.data);
//				}else{
//					_set_lable_text ('#computerTheRepayDetailSum2 span ','name','',false,result.data);
//				}
			}
		},
		complete:function(){
		}
	});

}


var initHtmlRepaymentDetailSpan = function(data,sign){
	var url = 'sys/LoanExt/findRepaymentDetailForSettleAccounts?queryProxy[id]='+_param_id;
	_set_lable_text('#currentRepayDetail span ','name',url,false,data,function(value,name){
		var theOne = $('#'+sign+' span[name="'+name+'"]'),expired=$('#expired'+name);
		if(typeof(theOne)!='undefined' && typeof(expired)!='undefined'){
			var expiredValue = expired.text();
			if(_is_empty(expiredValue)){
				expiredValue = '0';
			}
			var sum = parseInt(value,10) + parseInt(expiredValue,10);
			theOne.text(sum);
		}
	});
};

var initForm = function(){
	$('#clear_account_defult').form({
		url :'sys/LoanExt/clearingAccount',
		onBeforeLoad : function(param){
			$.messager.progress();
		},
		onLoadSuccess : function(data) {
			//加载完成之后才构建
			$.messager.progress("close");
		},
		onLoadError : function(data) {
			//加载完成之后才构建
			$.messager.progress("close");
			$.messager.alert('提示', "加载内容失败,请重新刷新页面/联系管理员", "error");
		},
		onSubmit : function(param) {
			if (!$(this).form("validate")) {
				$.messager.alert('提示', "提交的选项中,有错误内容,请检查", "error");
				return false;
			}
			$.messager.progress();
		},
		success : function(data) {	
			$.messager.progress("close");
			var jd = eval('(' + data + ')');
			if(!!!jd){
				$.messager.alert('提示', "未知异常", "error");
				return false;
			}
			if(jd.state!=200){
				$.messager.alert('提示', jd.message, "error");
				return false;
			}
			$.dialog.alert('结算成功!',function(){
				top._do_goto_right(parent.basePath+'jsp/loan/loan_repayment_detail_list.jsp');
			});

		}
	});
};


var initLoanRepaymentLog  = function(array){
	$('#enterprise').dataTable({
		"bProcessing" : true, //DataTables载入数据时，是否显示‘进度’提示
		"bServerSide" : true, //是否启动服务器端数据导入
		"bStateSave" : false, //是否打开客户端状态记录功能,此功能在ajax刷新纪录的时候不会将个性化设定回复为初始化状态
		"bJQueryUI" : true, //是否使用 jQury的UI theme
		"aLengthMenu" : [5, 10, 15], //更改显示记录数选项
		"iDisplayLength" : 10, //默认显示的记录数
		"bAutoWidth" : true, //是否自适应宽度 update
		"bScrollCollapse" : false, //是否开启DataTables的高度自适应，当数据条数不够分页数据条数的时候，插件高度是否随数据条数而改变
		"bPaginate" : false, //是否显示（应用）分页器
		"bInfo" : false, //是否显示页脚信息，DataTables插件左下角显示记录数
		"sPaginationType" : "full_numbers", //详细分页组，可以支持直接跳转到某页
		"bSort" : true, //是否启动各个字段的排序功能
		"aaSorting" : [[0, "ASC"]], //默认的排序方式，第3列，升序排列
		"bFilter" : false, //是否启动过滤、搜索功能
		"sPaginationType": "full_numbers",
		"sScrollX": '100%',
		"sScrollXInner": 1675,
		'destroy':true,
		"sAjaxSource":parent.basePath+"sys/LoanRepaymentLog/list",
		"fnServerData":function( sSource, aoData, fnCallback ) {
			$.merge(aoData,array);//合并
			$.unique(aoData);//去重
			$.ajax( {
				type: "POST", 
				url: sSource, 
				cache : false,
				dataType : "json",
				data : aoData, 
				success: function(resp) {
					if(resp && resp.state == 200){
						fnCallback(resp.datagrid);
						return false;
					}
					fnCallback(null);
				}
			});
		},  //自定义数据获取函数
		"aoColumns" : [ {
			"mDataProp" : "id",
			"sTitle" : "操作",
			"bSortable": false,
			"sDefaultContent" : "",
			"sClass" : "center",
			"sWidth":"100px",
			"mRender": function (data, display, row) {
				return _set_default_href(data);
			}
		},{
			"mDataProp" : "repayDate",
			"sTitle" : "收款时间",
			"bSortable": false,
			"sDefaultContent" : "",
			"sClass" : "center",
			"mRender": function (data, display, row) {
				if(_is_empty(data)){
					return '';
				}
				return new Date(data).format('yyyy-MM-dd');
			}
		}, {
			"mDataProp" : "repayUserName",
			"sTitle" : "收款人",
			"bSortable": false,
			"sDefaultContent" : "",
			"sClass" : "center",
			"mRender": function (data, display, row) {
				return data;
			}
		},{
			"mDataProp" : "repayAmount",
			"sTitle" : "收款金额",
			"bSortable": false,
			"sDefaultContent" : "",
			"sClass" : "center",
			"mRender": function (data, display, row) {
				return data+'(元)';
			}
		},{
			"mDataProp" : "payType",
			"sTitle" : "交易方式",
			"bSortable": false,
			"sDefaultContent" : "",
			"sClass" : "center",
			"mRender": function (data, display, row) {
				return top.SYS_BASE_DATA['Loan_Transaction_Mode_Code'][data];
			}
		}, {
			"mDataProp" : "payClientName",
			"sTitle" : "汇款人",
			"bSortable": false,
			"sDefaultContent" : "",
			"sClass" : "center"
		}, {
			"mDataProp" : "payBankName",
			"sTitle" : "汇款银行",
			"bSortable": false,
			"sDefaultContent" : "",
			"sClass" : "center",
			"mRender": function (data, display, row) {
				return top.SYS_BASE_DATA['Client_Bank_Name_Code'][data];
			}
		}, {
			"mDataProp" : "payBankAccount",
			"sTitle" : "汇款银行账户",
			"bSortable": false,
			"sDefaultContent" : "",
			"sClass" : "center"
		}, {
			"mDataProp" : "repayRemark",
			"sTitle" : "收款备注",
			"bSortable": false,
			"sDefaultContent" : "",
			"sClass" : "center",
			"mRender": function (data, display, row) {
				if(_is_empty(data)){
					return '';
				}
				return data.length>20?(data.substring(0,20)+'......'):data;
			}
		}
		],
		"oLanguage": {
			"sUrl":parent.basePath+"static_resources/language/lang-cn.txt" //这个是国际化，也就是汉化的各种文字数据源，写对url就行了
		}
	});
};


//编辑收费纪录
var default_edit = function(id){
	/* 方式一 在内容页里写代码取值 */
	/* 下面的代码为内容页value01.html页面里的代码，请自行打开此文件查看代码 */
	$.dialog({
		id:'edit_child_LoanRepaymentLog',
		lock:true,
		title:'编辑收费纪录',
		width:500,
		height:400,
		content:'url:'+parent.basePath+'jsp/loan/loan_repayment_income.jsp?id='+id+'&loanId='+loanId+'&repaymentId='+repaymentId+'&repaymentLogDetailId='+_param_id,
		close: function(){
			top._do_goto_right('jsp/loan/loan_repayment_detail_edit.jsp?id='+_param_id+'&loanId='+loanId+'&repaymentId='+repaymentId);

		}
	}).max();
};

//删除收费纪录
var default_del = function(id){
	$.dialog.confirm('你确定这个收费纪录吗？', function(){
		$.ajax({
			url:'sys/LoanExt/dropLoanRepaymentLog',
			data:{pks:id},
			type:'post',
			dataType:'json',
			success:function(result){
				if(result.state=="200"){
					$.dialog.alert('删除成功！',function(){
						top._do_goto_right('jsp/loan/loan_repayment_detail_edit.jsp?id='+_param_id+'&loanId='+loanId+'&repaymentId='+repaymentId);
					});
				}else{
					$.dialog.alert('删除失败！');
				}
			}
		});
	}, function(){
//		$.dialog.tips('执行取消操作');
	});
};



var clearFromData = function(){
	$("#_form_repayment_log_default_").form()[0].reset();
	$("#repaymentLogLoanId").val(loanId);
	$("#repaymentLogDetailId").val(_param_id);
	$("#repaymentLogId").val("");
};



